/*
 * @# PostRedis.java 2011-12-22 下午03:39:10
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.posts.redis;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;

import com.ianswer.px.common.Constants;
import com.ianswer.px.common.redis.RedisSupport;
import com.ianswer.px.posts.entity.Post;
import com.ianswer.px.posts.entity.PostRedisEntity;

/**
 * 博文在redis中的处理.
 * 
 * @author alvin hwang
 */
public class PostRedis extends RedisSupport {
	
	public static final String POST_TYPE_ORIGIN="origin";
	public static final String POST_TYPE_TRANSFER="transfer";
	
	/**
	 * 初始化redis中的post块
	 * @param post
	 */
	public static void postInit(Post post,long post_id) {
		Map<String,String> rd_post = new HashMap<String,String>();
		
		rd_post.put(Constants.RD_POST_F_ADDTIME,String.valueOf(post.getCreatedAt()));
		rd_post.put(Constants.RD_POST_F_TRANSFERREDTOTAL, "0");
		rd_post.put(Constants.RD_POST_F_TRANSFERRED, "0");
		rd_post.put(Constants.RD_POST_F_COMMENTSTOTAL, "0");
		rd_post.put(Constants.RD_POST_F_COMMENTS, "0");
		rd_post.put(Constants.RD_POST_F_VISIT, "0");
		//若是转发的，则记录原始的博文post_id
		if (POST_TYPE_TRANSFER.equals(post.getPostType()) && post.getTranid() != null) {
			rd_post.put(Constants.RD_POST_F_TRAN_ID, post.getTranid().toString());	
		}
		redis().hmset(Constants.RD_POST_KEY + post_id,rd_post);
		
	}

	public static PostRedisEntity get(long post_id) {
		
		Map<String,String> r = redis().hgetAll(Constants.RD_POST_KEY + post_id);
		
		if (MapUtils.isEmpty(r)) return null;
		
		PostRedisEntity e = new PostRedisEntity();
		e.setAddtime(r.get(Constants.RD_POST_F_ADDTIME));
		e.setVisit(r.get(Constants.RD_POST_F_VISIT));
		e.setFavor(r.get(Constants.RD_POST_F_FAVOR));
		e.setTransferredtotal(r.get(Constants.RD_POST_F_TRANSFERREDTOTAL));
		e.setTransferred(r.get(Constants.RD_POST_F_TRANSFERRED));
		e.setCommentstotal(r.get(Constants.RD_POST_F_COMMENTSTOTAL));
		e.setComments(r.get(Constants.RD_POST_F_COMMENTS));
		e.setTranid(r.get(Constants.RD_POST_F_TRAN_ID));
		return e;
	}
}
